package com.panfeng.xcloud.boss.provider.assets.service.impl;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.CoinListMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.WalletAssetsMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.WalletTransLogMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dto.WalletTransLogExtendItemDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.request.QueryWalletTransLogPageReqDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.response.QueryWalletTransLogPageRespDTO;
import com.panfeng.xcloud.boss.provider.assets.service.IWalletTransLogService;
import com.panfeng.xcloud.common.core.enums.CoinListStatusTypeEnum;
import com.panfeng.xcloud.common.core.enums.ExchangeTypeEnum;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.common.core.utils.KeyContactUtil;
import com.panfeng.xcloud.dao.assets.entity.CoinList;
import com.panfeng.xcloud.dao.assets.entity.WalletAssets;
import com.panfeng.xcloud.dao.base.MapperSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WalletTransLogServiceImpl extends MapperSupport<Integer, WalletAssets, WalletAssetsMapperExt> implements IWalletTransLogService {

    @Autowired
    WalletTransLogMapperExt walletTransLogMapperExt;
    @Autowired
    private CoinListMapperExt coinListMapperExt;

    /**
     * 查询相关交易日志
     * @param queryWalletTransLogPageReqDTO
     * @return
     */
    @Override
    public PageResponse<QueryWalletTransLogPageRespDTO> queryWalletTransLog(QueryWalletTransLogPageReqDTO queryWalletTransLogPageReqDTO) {
        PageResponse<WalletTransLogExtendItemDTO> pageResponse = super.selectPage(queryWalletTransLogPageReqDTO, walletTransLogMapperExt::queryWalletTransLogExtPageCount, walletTransLogMapperExt::queryWalletTransLogExtPage);

        List<WalletTransLogExtendItemDTO> items = pageResponse.getItems();
        log.info(">>>> queryWalletTransLog-items.list：{} <<<",(null != items ? JSON.toJSONString(items):""));
        List<QueryWalletTransLogPageRespDTO> queryWalletTransLogRespDTOS = ConvertUtils.convert(items, QueryWalletTransLogPageRespDTO.class);
        List<CoinList> coinList = coinListMapperExt.queryCoinListByExchangeType(ExchangeTypeEnum.HUOBI.getCode(), CoinListStatusTypeEnum.OPEN.code);
        Map<String, String> collectUrlMap = coinList.stream().collect(Collectors.toMap(CoinList::getCollectUrlMapKey, CoinList::getUrl));
        if (queryWalletTransLogRespDTOS != null)
        queryWalletTransLogRespDTOS.forEach(dto->{
            dto.setUrl(collectUrlMap.get(KeyContactUtil.getCollectUrlMapKey(dto.getDestinationCoin(),dto.getDirection())));
        });

        PageResponse<QueryWalletTransLogPageRespDTO> pageFinalResponse = new PageResponse();
        pageFinalResponse.setTotalCount(pageResponse.getTotalCount());
        pageFinalResponse.measureTotalPage(pageResponse.getTotalCount(),pageResponse.getPageSize());
        pageFinalResponse.setItems(queryWalletTransLogRespDTOS);
        pageFinalResponse.setPageNumber(pageResponse.getPageNumber());
        pageFinalResponse.setPageSize(pageResponse.getPageSize());

        return pageFinalResponse;
    }

}
